package com.doublefish.doublefishrobot.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者:dugaofeng on 2024/10/16 08:58
 */
public class WebActivity extends BaseActivity {

    @BindView( R.id.tv_title)
    TextView titleTv;

    @BindView(R.id.webview)
    WebView webview;

    private String tittle = null;
    //跳转
    public static Intent makeIntent(Context packageContext, String url, String tittle) {
        Intent intent = new Intent(packageContext, WebActivity.class);
        intent.putExtra("url", url);
        if (!TextUtils.isEmpty(tittle)) {
            intent.putExtra("title", tittle);
        }
        return intent;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected void initView() {

        initWebView();
    }

    @Override
    protected void initData() {
        tittle = getIntent().getStringExtra("title");
        if (!TextUtils.isEmpty(tittle)) {
            titleTv.setText(tittle);
        }

        String url = getIntent().getStringExtra("url");
        webview.loadUrl(url);
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.ll_back)
    public void onBackBtnClick() {
        finish();
    }

    private void initWebView() {
        if (webview == null)
            return;

        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        // webSettings.setPluginsEnabled(true);
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

//        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

        // 允许弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        webSettings.setAllowFileAccess(true);

        webSettings.setDomStorageEnabled(true);
//        webSettings.setAppCacheEnabled(true);
        webSettings.setDatabaseEnabled(true);

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (TextUtils.isEmpty(tittle)) {
                    //延迟1秒获取title
                    getWebTitleDelay();
                }
            }
        });
    }

    private void getWebTitleDelay() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                titleTv.setText(webview.getTitle());
            }
        }, 1000);
    }
}
